<?php
class Calendar_ReservationController extends Promotor_Controller_Action_Scaffold {
	
	public $skin = array(
		// forsuje domyslny template i style na admin
		'template' => 'admin',
		'style' => 'default',
		// layout
		'layout' => 'admin_calendar',
		// rozszerzam konfiguracje
		'config' => array(
			'filename' => 'backend_config.ini'
		),
		
		// specjalizuje template dla akcji
		'index' => array(
			'layout' => 'calendar_reservation'
		)
	);
	
	public $acl = array(
		'index' => 'show',
		'list' => 'list',
		'add' => 'create',
		'edit' => 'update',
		'delete' => 'delete'
	);

	/**
	 * @see Promotor_Controller_Action_Scaffold specialization
	 */
	protected $_modelClass = 'Calendar_Model_Reservation';
	protected $_formAddClass = 'Calendar_Form_ReservationAdd';
	protected $_formEditClass = 'Calendar_Form_ReservationAdd';
	protected $_formRemoveClass = 'Calendar_Form_ReservationRemove';
	
	/**
	 * @return void
	 */
	public function indexAction() {
		$year = $this->_getParam('year', date('Y'));
		$this->view->year = $year;

		// get data model
		$model = new Calendar_Model_Reservation();
		$this->view->fromto = $model->getFromTo($year);
		
		// setup calendar
		$calendar = new KontorX_Calendar(mktime(1,1,1,1,1,$year));
		$this->view->months = $calendar->getMonths();

		$translator = new Zend_Translate('tmx', APP_LANGUAGE_PATHNAME . '/tmx/calendar.xml', $this->_helper->system->language());
		$this->view->translate()->setTranslator($translator);
	}
	
	/**
	 * @return void
	 */
	public function listAction() {
		$model = new Calendar_Model_Reservation();
		$table = $model->getDbTable();

		$rq = $this->getRequest();
		if ($rq->isPost()) {
			switch ($rq->getPost('action_type')) {
				case 'update':
					if (null !== $rq->getPost('editable')) {
						if ($this->_helper->acl->isAllowed('update')) {
							$data = $rq->getPost('editable');
							$model->editableUpdate($data);
							$this->_helper->flashMessenger($model->getStatus());
						}
					}
					break;
				case 'delete':
					if (null !== $rq->getPost('action_checked')) {
						if ($this->_helper->acl->isAllowed('delete')) {
							$data = $rq->getPost('action_checked');
							$model->editableDelete($data);
							$this->_helper->flashMessenger($model->getStatus());
						}
					}
					break;
			}			
		}

		// setup data grid
		$config = $this->_helper->config('reservation.xml');
		$grid = KontorX_DataGrid::factory($table, $config->grid);
		$grid->setValues($this->_getAllParams());

		$this->view->grid = $grid;
	}
}